package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@Api(tags = "菜品管理")
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishServiceImpl dishServiceImpl;
    //新增菜品
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        Set keys = redisTemplate.keys("dish_*");
        log.info("删除缓存{}", keys);
        redisTemplate.delete(keys);
        dishServiceImpl.saveWithFlavor(dishDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询");
        PageResult pageResult = dishServiceImpl.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping("")
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        Set keys = redisTemplate.keys("dish_*");
        log.info("删除缓存{}", keys);
        redisTemplate.delete(keys);
        log.info("删除菜品{}", ids);
        dishServiceImpl.deleteBatch(ids);
        return Result.success();
    }
    @ApiOperation("修改菜品状态")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        log.info("批量修改菜品状态{},{}",id, status);
        dishServiceImpl.update(status, id);
        return Result.success();
    }
    @ApiOperation("菜品查询回显")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("菜品查询回显{}", id);
        DishVO dishVO = dishServiceImpl.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        //删除单一菜品缓存
        Long categoryId = dishDTO.getCategoryId();
        redisTemplate.delete("dish_" + categoryId);
        log.info("修改菜品{}", dishDTO);
        dishServiceImpl.updateWithFlavor(dishDTO);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据categoryId查询菜品信息")
    public Result<List<Dish>> list(@RequestParam Long categoryId ) {
        log.info("根据categoryId查询菜品信息{}", categoryId);
        java.util.List<Dish>dishList =dishServiceImpl.listDish(categoryId);
        return Result.success(dishList);
    }

}
